package com.example.java_gobang.config;

import com.example.java_gobang.api.GameAPI;
import com.example.java_gobang.api.MatchAPI;
import com.example.java_gobang.api.TestAPI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor;

@Configuration//定义一个配置类,交给spring管理(把该类作为spring的xml配置文件中的:作用为配置spring容器(应用上下文))
@EnableWebSocket//让spring框架知道这个类是配置webSocket的类
//这个类的目的是为了让testAPI与"/test"这个路径关联起来
public class WebSocketConfig implements WebSocketConfigurer {
    @Autowired
    private TestAPI testAPI;

    @Autowired
    private MatchAPI matchAPI;

    @Autowired
    private GameAPI gameAPI;
    @Override
    //注册一些handler到框架中
    public void registerWebSocketHandlers(WebSocketHandlerRegistry webSocketHandlerRegistry) {
        //这样就可以调用TestAPI中方法.约定了testAPI类与test路径相匹配
        //webSocketHandlerRegistry.addHandler(testAPI,"/test");//spring注入的方式
        //webSocketHandlerRegistry.addHandler(new testAPI(),"/test");

        //匹配机制的模块
        webSocketHandlerRegistry.addHandler(matchAPI,"/findMatch")
                //把HttpSession搞到Websocket的session中,就可以拿到登录时候的信息了
                .addInterceptors(new HttpSessionHandshakeInterceptor());

        //对战模块
        webSocketHandlerRegistry.addHandler(gameAPI,"/game")
                .addInterceptors(new HttpSessionHandshakeInterceptor());

    }
}
